<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <style>
        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }
    </style>
    <title>Hello, World</title>
    <style type="text/css">
        #container {
            height: 1000px;
            width: 1500px;
        }
    </style>
    <style>
        .tangram-suggestion {
            z-index: 9999;
        }
    </style>

    <script type="text/javascript" src="http://api.map.baidu.com/api?v=3.0&ak=RDT7YXdHRMt0rDt0obNNGVEcm9bjxXG1">
        //v3.0版本的引用方式：src="http://api.map.baidu.com/api?v=3.0&ak=您的密钥"
    </script>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>工作台
            <small>地址管理</small>
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>地址管理</el-breadcrumb-item>
            <el-breadcrumb-item>地址管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <div>
                    <el-tabs v-model="activeName" type="card">
                        <el-tab-pane label="分布图" name="first">
                            <div id="container">
                            </div>
                        </el-tab-pane>
                        <el-tab-pane label="详细信息" name="second">
                            <el-input placeholder="请输入内容" v-model="pagination.queryString"
                                      style="width: 200px;"></el-input>
                            <el-button @click="findPage()">查询</el-button>
                            <el-button type="primary" @click="handleCreate()">新建</el-button>
                            <template>
                                <el-table
                                        current-row-key="id"
                                        :data="dataList"
                                        stripe highlight-current-row
                                        style="width: 100%">
                                    <el-table-column
                                            type="index"
                                            label="序号"
                                            width="180" align="center">
                                    </el-table-column>
                                    <el-table-column
                                            prop="name"
                                            label="公司地址"
                                            width="180" align="center">
                                    </el-table-column>
                                    <el-table-column
                                            prop="lng"
                                            label="所在经度" align="center">
                                    </el-table-column>
                                    <el-table-column
                                            prop="lat"
                                            label="所在纬度" align="center">
                                    </el-table-column>
                                    <el-table-column
                                            label="操作" align="center">
                                        <template slot-scope="scope">
                                            <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除
                                            </el-button>
                                        </template>
                                    </el-table-column>
                                </el-table>
                                <el-pagination
                                        @current-change="handleCurrentChange"
                                        :current-page="pagination.currentPage"
                                        :page-size="pagination.pageSize"
                                        layout="total, prev, pager, next, jumper"
                                        :total="pagination.total">
                                </el-pagination>
                            </template>
                        </el-tab-pane>
                    </el-tabs>
                    <!-- 新增标签弹层 -->
                    <div>
                        <el-dialog title="新建地址" :visible.sync="dialogFormVisible" @opened="opened">
                            <div style="width: 500px;height: 700px">
                                <div id="r-result"><input type="text" id="suggestId" size="20" style="width:200px; height: 28px"/>
                                    <el-button type="primary" @click="add()">确定</el-button>
                                </div>
                                <div id="searchResultPanel"
                                     style="border:1px solid #C0C0C0;width:150px;height:auto; display:none;"></div>
                                <div id="l-map" style="width: 550px;height: 450px"></div>
                            </div>
                        </el-dialog>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>

<script>
    var vue = new Vue({
        el: '#app',

        data: {
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 10,//每页显示的记录数
                total: 0,//总记录数
                queryString: null//查询条件

            },
            value: {
                name: '',
                lng: '',
                lat: '',
            },
            dataList: [],//当前页要展示的分页列表数据
            activeName: 'first',//添加/编辑窗口Tab标签名称,
            dialogFormVisible: false,//增加表单是否可见
            ListAddress: [],

        },
        created() {
            // 分页查询
            this.findPage();
            this.findAll();
        },
        mounted() {
            this.map();
        },
        methods: {
            map() {
                // 百度地图API功能
                var map = new BMap.Map("container");
                map.centerAndZoom(new BMap.Point(114.328749, 30.60), 13);
                map.enableScrollWheelZoom(true);
                var index = 0;
                var myGeo = new BMap.Geocoder();
                var adds = new Array();
                for (let i = 0; i < this.ListAddress.length; i++) {
                    adds.push(new BMap.Point(this.ListAddress[i].lng, this.ListAddress[i].lat));
                }

                for (var i = 0; i < adds.length; i++) {
                    var marker = new BMap.Marker(adds[i]);
                    map.addOverlay(marker);
                    marker.setLabel(new BMap.Label(this.ListAddress[i].name, {offset: new BMap.Size(20, -10)}));
                }

                function bdGEO() {
                    var pt = adds[index];
                    geocodeSearch(pt);
                    index++;
                }

                function geocodeSearch(pt) {
                    if (index < adds.length - 1) {
                        setTimeout(window.bdGEO, 400);
                    }
                    myGeo.getLocation(pt, function (rs) {
                        var addComp = rs.addressComponents;
                        document.getElementById("result").innerHTML += index + ". " + adds[index - 1].lng + "," + adds[index - 1].lat + "：" + "商圈(" + rs.business + ")  结构化数据(" + addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber + ")<br/><br/>";
                    });
                }


            },
            opened() {
                // 百度地图API功能
                function G(id) {
                    return document.getElementById(id);
                }

                var map = new BMap.Map("l-map");
                map.centerAndZoom("武汉", 12); // 初始化地图,设置城市和地图级别。
                map.enableScrollWheelZoom(true);

                var ac = new BMap.Autocomplete(    //建立一个自动完成的对象
                    {
                        "input": "suggestId"
                        , "location": map
                    });

                ac.addEventListener("onhighlight", function (e) {  //鼠标放在下拉列表上的事件
                    var str = "";
                    var _value = e.fromitem.value;
                    var value = "";
                    if (e.fromitem.index > -1) {
                        value = _value.province + _value.city + _value.district + _value.street + _value.business;
                    }
                    str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;

                    value = "";
                    if (e.toitem.index > -1) {
                        _value = e.toitem.value;
                        value = _value.province + _value.city + _value.district + _value.street + _value.business;
                    }
                    str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
                    G("searchResultPanel").innerHTML = str;
                });

                var myValue;
                ac.addEventListener("onconfirm", function (e) {    //鼠标点击下拉列表后的事件
                    var _value = e.item.value;
                    myValue = _value.province + _value.city + _value.district + _value.street + _value.business;
                    G("searchResultPanel").innerHTML = "onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;
                    vue.value.name = myValue;
                    setPlace();
                });

                function setPlace() {
                    map.clearOverlays();    //清除地图上所有覆盖物
                    function myFun() {
                        var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
                        map.centerAndZoom(pp, 18);
                        map.addOverlay(new BMap.Marker(pp));    //添加标注
                        console.log(pp)
                        vue.value.lat = pp.lat;
                        vue.value.lng = pp.lng;
                    }

                    var local = new BMap.LocalSearch(map, { //智能搜索
                        onSearchComplete: myFun
                    });
                    local.search(myValue);
                }
            },

            findPage() {
                var param = {
                    currentPage: this.pagination.currentPage,//页码
                    pageSize: this.pagination.pageSize,//每页显示的记录数
                    queryString: this.pagination.queryString//查询条件
                }
                //发放ajax请求
                axios.post("/address/findPage.do", param).then((res) => {
                    this.pagination.total = res.data.total;
                    this.dataList = res.data.rows;
                })
            },
            // 删除
            handleDelete(row) {
                alert(row.id)
                // 提示用户是否删除
                this.$confirm("你确定要删除吗", "提示", {
                    type: "warning"
                }).then(() => {
                    //发送ajax请求删除
                    axios.get("/address/delete.do?id=" + row.id).then((res) => {
                        if (res.data.flag) {
                            //删成功并提示信息
                            this.$message({
                                type: 'successs',
                                message: res.data.message
                            });
                            //重新进行分页查询
                            this.findPage();
                        } else {
                            this.$message.error(res.data.message)
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '操作已取消'
                    })
                })

            },
            // 弹出添加窗口
            handleCreate() {
                this.dialogFormVisible = true;
                //this.map2()
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;

            },
            add() {
                this.dialogFormVisible = false;
                axios.post("/address/add.do", vue.value).then((res) => {
                    console.log(vue.value)
                    if (res.data.flag) {
                        this.findPage();
                        //弹出提示信息
                        this.$message({
                            message: res.data.message,
                            type: 'success'
                        });
                    } else {
                        //弹出提示
                        this.$message.error(res.data.message);
                    }
                })
            },
            findAll() {
                axios.get("/address/findAll.do").then((res) => {
                    if (res.data.flag) {
                        this.ListAddress = res.data.data;
                        this.map();
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
            }

        }
    })
</script>
</html>


